/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jauker.badgeview.example.view.indicator;

import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.global.resource.solidxml.Theme;
import ohos.global.resource.solidxml.TypedAttribute;

import com.jauker.badgeview.example.util.ResUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PageSliderIndicatorExt extends ComponentContainer {

    private static final String TAG = PageSliderIndicatorExt.class.getSimpleName();

    //custom attributes
    private static final String indicator_normalColor = "indicator_normalColor";
    private static final String indicator_selectedColor = "indicator_selectedColor";
    private static final String indicator_strokeColor = "indicator_strokeColor";
    private static final String indicator_normalStrokeWidth = "indicator_normalStrokeWidth";
    private static final String indicator_selectedStrokeWidth = "indicator_selectedStrokeWidth";

    private PageSliderIndicator mPageSliderIndicator;
    private int normalColor, selectedColor, strokeColor, normalStrokeWidth, selectedStrokeWidth;

    public PageSliderIndicatorExt(Context context) {
        this(context, null);
    }

    public PageSliderIndicatorExt(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public PageSliderIndicatorExt(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initPageSliderIndicator(context, attrSet);
    }

    private void initPageSliderIndicator(Context context, AttrSet attrSet) {
        mPageSliderIndicator = new PageSliderIndicator(context);
        DirectionalLayout.LayoutConfig indicatorLayoutConfig = new
                DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        indicatorLayoutConfig.alignment = LayoutAlignment.CENTER;
        mPageSliderIndicator.setLayoutConfig(indicatorLayoutConfig);
        addComponent(mPageSliderIndicator);

        if (attrSet.getAttr(indicator_normalColor).isPresent()) {
            normalColor = attrSet.getAttr(indicator_normalColor).get().getColorValue().getValue();
        }

        if (attrSet.getAttr(indicator_selectedColor).isPresent()) {
            selectedColor = attrSet.getAttr(indicator_selectedColor).get().getColorValue().getValue();
        }

        if (attrSet.getAttr(indicator_strokeColor).isPresent()) {
            strokeColor = attrSet.getAttr(indicator_strokeColor).get().getColorValue().getValue();
        }

        if (attrSet.getAttr(indicator_normalStrokeWidth).isPresent()) {
            normalStrokeWidth = attrSet.getAttr(indicator_normalStrokeWidth).get().getDimensionValue();
        }

        if (attrSet.getAttr(indicator_selectedStrokeWidth).isPresent()) {
            selectedStrokeWidth = attrSet.getAttr(indicator_selectedStrokeWidth).get().getDimensionValue();
        }
    }

    public void setPageSlider(PageSlider pageSlider) {
        mPageSliderIndicator.setPageSlider(pageSlider);
    }

    public void setItemOffset(int offset) {
        mPageSliderIndicator.setItemOffset(offset);
    }

    public void setItemElement(Element normal, Element selected) {
        mPageSliderIndicator.setItemElement(normal, selected);
    }

    public void setCircleStyleViaLayout() {
        setItemElement(
                ResUtil.getCustomCircleGradientDrawable(normalStrokeWidth, new Color(normalColor), Color.BLACK, new Rect(0, 0, 50, 50)),
                ResUtil.getCustomCircleGradientDrawable(selectedStrokeWidth, new Color(selectedColor), Color.BLACK, new Rect(0, 0, 50, 50)));
    }

    public void setCircleStyleViaThemes(int theme) {
        try {
            Theme circleTheme = getResourceManager().getTheme(theme);
            HashMap<String, TypedAttribute> circleAttributes = circleTheme.getThemeHash();
            for (Map.Entry<String, TypedAttribute> entry : circleAttributes.entrySet()) {
                if (entry.getValue().getName().equals(indicator_normalColor)) {
                    normalColor = entry.getValue().getColorValue();
                }
                if (entry.getValue().getName().equals(indicator_selectedColor)) {
                    selectedColor = entry.getValue().getColorValue();
                }
                if (entry.getValue().getName().equals(indicator_strokeColor)) {
                    strokeColor = entry.getValue().getColorValue();
                }
                if (entry.getValue().getName().equals(indicator_normalStrokeWidth)) {
                    normalStrokeWidth = entry.getValue().getIntegerValue();
                }
                if (entry.getValue().getName().equals(indicator_selectedStrokeWidth)) {
                    selectedStrokeWidth = entry.getValue().getIntegerValue();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        setItemElement(
                ResUtil.getCustomCircleGradientDrawable(normalStrokeWidth, new Color(normalColor), Color.BLACK, new Rect(0, 0, 50, 50)),
                ResUtil.getCustomCircleGradientDrawable(selectedStrokeWidth, new Color(selectedColor), Color.BLACK, new Rect(0, 0, 50, 50)));
    }

    public void setLineStyleViaLayout() {
        setItemElement(
                ResUtil.getCustomRectGradientDrawable(new Color(normalColor), new Rect(0, 0, 70, 12)),
                ResUtil.getCustomRectGradientDrawable(new Color(selectedColor), new Rect(0, 0, 70, 12)));
    }

    public void setTabStyleViaLayout() {
        setItemElement(
                ResUtil.getCustomRectGradientDrawable(new Color(normalColor), new Rect(0, 0, 270, 4)),
                ResUtil.getCustomRectGradientDrawable(new Color(selectedColor), new Rect(0, 0, 270, 10)));
    }
}
